package com.spike.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spike.service.UserService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spike.mapper.UserMapper;
import com.spike.model.User;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
@AllArgsConstructor
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private UserMapper userMapper;
    @Override
    public Integer register(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhoneNumber, user.getPhoneNumber());
        Long l = userMapper.selectCount(wrapper);
        if (l == 0) {
//            可以注册
            userMapper.register(user);
            Integer userId = user.getUserId();
            return userId;
        }else
            return -1;
    }

    @Override
    public Boolean login(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());
        wrapper.eq(User::getPassword, user.getPassword());
        User user1 = userMapper.selectOne(wrapper);
        if (user1 != null) {
            return true;
        }else
            return false;
    }

    @Override
    public Boolean update(Integer userId,User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserId, userId);
        wrapper.eq(User::getVersion, user.getVersion());
        int update = userMapper.update(user, wrapper);
        if (update == 1) {
            return true;
        }else
            return false;
    }

    @Override
    public Boolean delete(Integer userId, Integer version) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserId, userId);
        wrapper.eq(User::getVersion, version);
        int delete = userMapper.delete(wrapper);
        if (delete == 1) {
            return true;
        }else
            return false;
    }
}
